/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.data.view;

import java.io.IOException;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import offset.nodes.Constants;
import offset.nodes.client.virtual.model.jcr.SimpleNode;
import offset.nodes.server.view.ContainerBean;
import offset.nodes.server.dispatch.controller.DispatchAttribute;
import offset.nodes.server.view.list.BeforeUpdateModelListManager;
import offset.nodes.server.view.list.ListManager;
import offset.nodes.server.model.RepositoryModel;
import offset.nodes.server.model.RepositoryUtils;
import offset.nodes.server.reference.view.ReferenceBean;
import offset.nodes.server.version.view.VersionBean;
import offset.nodes.server.view.FacesUtils;
import offset.nodes.server.view.component.applet.AppletModel;

/**
 *
 * @author Walter Lütgenau
 */
public class DataBean extends ContainerBean {
    String path;
    protected Node node;


    protected void initComponents() {
        AppletModel newModel = new AppletModel();
        newModel.setRendered(false);
        getComponents().put("new", newModel);

        AppletModel removeModel = new AppletModel();
        removeModel.setRendered(false);
        getComponents().put("remove", removeModel);

        AppletModel moveModel = new AppletModel();
        moveModel.setRendered(false);
        getComponents().put("move", moveModel);

        AppletModel copyModel = new AppletModel();
        copyModel.setRendered(false);
        getComponents().put("copy", copyModel);

        AppletModel orderModel = new AppletModel();
        orderModel.setRendered(false);
        getComponents().put("order", orderModel);

        AppletModel exportModel = new AppletModel();
        exportModel.setRendered(false);
        getComponents().put("export", exportModel);

        AppletModel exportFileModel = new AppletModel();
        exportFileModel.setRendered(false);
        getComponents().put("exportFile", exportFileModel);

        AppletModel importModel = new AppletModel();
        importModel.setRendered(false);
        getComponents().put("import", importModel);

        AppletModel acModel = new AppletModel();
        acModel.setRendered(false);
        getComponents().put("editAC", acModel);
    }
    
    protected void init() throws RepositoryException {
         DispatchAttribute att = (DispatchAttribute) FacesUtils.getRequest().getAttribute(DispatchAttribute.ATT_DISPATCH);
         if ( att == null) {
             path = FacesUtils.getPath();
             // supply dummy for phases before update model
             if (path == null) {
                propertiesManager = new BeforeUpdateModelListManager();
                nodesManager = new BeforeUpdateModelListManager();
                return;
             }

             // reinit from browser page
             if (node == null) {
                 node = RepositoryUtils.getRepositoryModel().getNode(path);
                 propertiesManager = new PropertiesListManager(node);
                 nodesManager = new NodesListManager(node);
                 // todo: init using the Before... managers.
                 return;
             }

             return;
         }
         
         // init from struts action
         path = att.getPath();
         node = att.getNode();
         if (node == null)
             node = new SimpleNode("");
         propertiesManager = new PropertiesListManager(node);
         nodesManager = new NodesListManager(node);
         FacesUtils.getRequest().removeAttribute(DispatchAttribute.ATT_DISPATCH);
    }

    public void renderNew(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("new");
        appletModel.setRendered(true);
    }

    public void renderRemove(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("remove");
        appletModel.setRendered(true);
    }

    public void renderMove(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("move");
        appletModel.setRendered(true);
    }

    public void renderCopy(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("copy");
        appletModel.setRendered(true);
    }

    public void renderOrder(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("order");
        appletModel.setRendered(true);
    }

    public void renderExport(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("export");
        appletModel.setRendered(true);
    }

    public void renderExportFile(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("exportFile");
        appletModel.setRendered(true);
    }

    public void renderImport(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("import");
        appletModel.setRendered(true);
    }

    public void renderEditAC(ActionEvent e) throws RepositoryException, IOException {
        AppletModel appletModel = (AppletModel) getComponents().get("editAC");
        appletModel.setRendered(true);
    }

    public boolean isAccessControl() throws RepositoryException {
        init();

        if (node == null)
            return true;

        return node.getPrimaryNodeType().getName().equals(Constants.TYPENAME_ACCESS_CONTROL);
    }

    public boolean isFile() throws RepositoryException {
        init();

        if (node == null)
            return true;

        return node.getPrimaryNodeType().getName().equals(Constants.TYPE_NODES_FILE);
    }

    /**
     * Remove an access control node
     * 
     * @param e
     * @throws RepositoryException
     * @throws IOException
     */
    public void removeAC(ActionEvent e) throws RepositoryException, IOException {
        RepositoryModel model = RepositoryUtils.getRepositoryModel();
 
        String path = getPath();
        int superior = path.lastIndexOf("/");
        if (superior >= 0) {
            path = path.substring(0, superior);
            model.removeMixin(path, Constants.TYPENAME_ACCESS_CONTROLLER);
        }
        
        String redirect = FacesUtils.getRepositoryPath()+path;
        FacesContext.getCurrentInstance().getExternalContext().redirect(redirect);
    }

   public boolean isVersioned() throws RepositoryException {
       init();
       if (node == null || node instanceof SimpleNode)
           return false;
       
       return node.isNodeType(Constants.TYPE_VERSIONABLE);
   }

   public String version() throws RepositoryException {
        DispatchAttribute dispatchInfo = new DispatchAttribute();
        dispatchInfo.setPath(getPath());
        dispatchInfo.setNode(node);
        dispatchInfo.setContentType(VersionBean.CONTENT_TYPE);
        FacesUtils.getRequest().setAttribute(DispatchAttribute.ATT_DISPATCH, dispatchInfo);

        return "versions";
    }

   public boolean isReferencable() throws RepositoryException {
       init();
       if (node == null || node instanceof SimpleNode)
           return false;

       return node.isNodeType(Constants.JCR_REFERENCEABLE);
   }

    public String references() throws RepositoryException {
        DispatchAttribute dispatchInfo = new DispatchAttribute();
        dispatchInfo.setPath(getPath());
        dispatchInfo.setNode(node);
        dispatchInfo.setContentType(ReferenceBean.CONTENT_TYPE);
        FacesUtils.getRequest().setAttribute(DispatchAttribute.ATT_DISPATCH, dispatchInfo);

        return "references";
    }

    public Node getNode() throws RepositoryException {
        init();
        return node;
    }

    public String getPath() throws RepositoryException {
        init();
        return path;
    }

    public String getIconContentType() {
        return Constants.CONTENT_TYPE_ICON;
    }

    protected ListManager propertiesManager;
    public ListManager getPropertiesManager() throws RepositoryException {
        init();
        return propertiesManager;
    }
    
    protected ListManager nodesManager;
    public ListManager getNodesManager() throws RepositoryException {
        init();
        return nodesManager;
   }
}
